# -*- coding: utf-8 -*-

import hashlib
from base import UserCase, Runner, TestSuite


class Case(UserCase):
    def create_user(self):
        p = {
            'name': self.randhan(3),
            'type': 'normal',
            'password': hashlib.md5(self.randchar(6)).hexdigest(),
            'username': self.randchar(8),
        }
        res = self.c.open('/api/user/create', p)
        return res['data']

    def create_device(self):
        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
            'corp': '',
        }
        res = self.c.open('/api/device/create', p)
        return res['data']

    def test_user_device_create(self):
        user = self.create_user()
        device = self.create_device()

        res = self.c.open('/api/user-device/list', {'user': user['id']})
        self.assertEqual(res['data']['count'], 0)

        res = self.c.open('/api/user-device/create', {'device': device['id'], 'user': user['id']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/user-device/list', {'user': user['id']})
        self.assertEqual(res['data']['count'], 1)


    def test_user_device_list(self):
        user = self.create_user()
        device = self.create_device()

        res = self.c.open('/api/user-device/list', {'user': user['id']})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 0)


    def test_user_device_delete(self):
        user = self.create_user()
        device = self.create_device()

        res = self.c.open('/api/user-device/create', {'device': device['id'], 'user': user['id']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/user-device/list', {'user': user['id']})
        self.assertEqual(res['code'], 0)

        self.assertEqual(res['data']['count'], 1)

        self.assertEqual(res['data']['itemList'][0]['device_obj']['id'], device['id'])

        res = self.c.open('/api/user-device/delete', {'user': user['id'], 'device': device['id']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/user-device/list', {'user': user['id']})
        self.assertEqual(res['data']['count'], 0)


    def test_user_device_list_filter(self):
        user = self.create_user()

        device = self.create_device()
        res = self.c.open('/api/user-device/create', {'device': device['id'], 'user': user['id']})

        device = self.create_device()
        res = self.c.open('/api/user-device/create', {'device': device['id'], 'user': user['id']})

        device = self.create_device()
        res = self.c.open('/api/user-device/create', {'device': device['id'], 'user': user['id']})

        res = self.c.open('/api/user-device/list')
        self.assertEqual(res['data']['count'] > 3, True)

        res = self.c.open('/api/user-device/list', {'user': user['id']})
        self.assertEqual(res['data']['count'], 3)



test_list = [
    'test_user_device_create',
    'test_user_device_list',
    'test_user_device_delete',
    'test_user_device_list_filter',
]
Suite = TestSuite([Case(t) for t in test_list])


if __name__ == '__main__':
    Runner.run(Suite)



